let Level = require('Level');
let soundMgr = require('soundMgr');
let dragons = require('dragons');
let nodeMgr = require('nodeMgr');
let DragTips = require('DragTips');

let actions = require('actions');
cc.Class({
    extends: Level,

    properties: {
        isNow:0,
        nowTime:0,
        nextLevelId:2,
    },
    /**
     * 开始关卡
     */
    startLevel(){
        let self = this;
        //禁止事件的拖拽
        nodeMgr.disableTouch();
        // self.allDongWuOut();
        // nodeMgr.enableTouch();
    },
    allDongWuOut(){
        let self = this;
        self.dongwulist = [[0,1],[0,1],[0,1],[2,3],[2,3],[2,3],[4],[4]];
        actions.fadeOut(self.dongwu[0],0.5);
        actions.fadeOut(self.dongwu[1],0.5);
        actions.fadeOut(self.dongwu[2],0.5);
        actions.fadeOut(self.dongwu[3],0.5);
        actions.fadeOut(self.dongwu[4],0.5);
        self._nextQuestion();
    },
    _nextQuestion(){
        let self = this;
        self.xiaoguan = 0;
        if(self.isNow > 0){
            nodeMgr.enableTouch();
            if(self.isNow == 3){
                actions.fadeOut(self.dongwu[0],0.5);
                actions.fadeOut(self.dongwu[1],0.5);
            }
            if(self.isNow == 5){
                self.rongqi.children[0].y = 900;
                self.rongqi.children[1].y = 900;
                self.rongqi.children[5].y = 8;
                self.rongqi.children[6].y = 8;
            }
            if(self.isNow == 6){
                actions.fadeOut(self.dongwu[2],0.5);
                actions.fadeOut(self.dongwu[3],0.5);
                self.rongqi.children[5].y = 900;
                self.rongqi.children[6].y = 900;
                self.rongqi.children[2].y = 8;
            }
            if(self.isNow == 7){
                self.rongqi.children[2].y = 900;
                self.rongqi.children[3].y = 8;
                self.rongqi.children[4].y = 8;
            }
            actions.fadeOut(self.dipan1,0.4);
            actions.fadeOut(self.panzi1,0.4);
            actions.fadeOut(self.dipan2,0.4);
            actions.fadeOut(self.panzi2,0.4);
            for (var i = 0; i < self["answers" + (self.isNow - 1)].length;i++){
                self["answers" + (self.isNow - 1)][i].active = false;
                self["answers" + (self.isNow - 1)][i].getComponent("DragNode").isMoveEnable = false
            }
        }
        for(var i = 0; i < self.dongwulist[self.isNow].length;i++){
            var index = self.dongwulist[self.isNow][i];
            actions.fadeIn(self.dongwu[index],0.5,function () {
                dragons.playAnimation(self.dongwu[index],"idle",0);
                self.dongwu[index].active = true;
            });
        }
        actions.fadeIn(self.zhuozi,0.5);
        if(self.isNow == 6){
            actions.fadeIn(self.dipan1,0.5);
            actions.fadeIn(self.panzi1,0.5);
        }else if(self.isNow == 7){
            actions.fadeIn(self.dipan2,0.5);
            actions.fadeIn(self.panzi2,0.5);
        }else if(self.isNow == 0){
            actions.fadeIn(self.dipan1,0.5);
            actions.fadeIn(self.panzi2,0.5);
        }else {
            actions.fadeIn(self.dipan2,0.5);
            actions.fadeIn(self.panzi2,0.5);
        }
        actions.fadeIn(self.yun[self.isNow],0.5,function () {
            for(var i = 0;i < self.yun[self.isNow].children.length;i++){
                for(var j = 0;j < self.yun[self.isNow].children[i].children.length;j++){
                    dragons.playAnimation(self.yun[self.isNow].children[i].children[j],self.yun[self.isNow].children[i].children[j].name + "_shine",3);
                }
            }
            soundMgr.playSoundEffectByName('cshuxue02_26',function () {
                if(self.isNow == 0){
                    self._delayShowTips();
                }
                nodeMgr.enableTouch();
                self.nowTime = (new Date()).getTime();
            });
        });
        for (var i = 0; i < self["answers" + self.isNow].length;i++){
            self["answers" + self.isNow][i].active = true;
            self["answers" + self.isNow][i].getComponent("DragNode").isMoveEnable = true;
            actions.fadeIn(self["answers" + self.isNow][i],0.5);
        }

    },
    _delayShowTips(){
        let self = this;
        //开启计时器
        self.scheduleOnce(self._showTips,self.tipsDelay);
    },
    _showTips(){
        let self = this;
        //显示手指
        self.tipsNode.node.active = true;
    },
    _hideTips(){
        let self = this;
        //显示关闭
        self.tipsNode.node.active = false;
        //停止计时器
        self.unschedule(self._showTips);
    },
    onDragStart(data){
        let self = this;
        this.nowTime = (new Date()).getTime() + 12000;
        //拖拽的时候停止手指动画
        self._hideTips();
    },
    eatAction(name){
        let self = this;
        if(self.isEatIndex == self.foods.length){
            // self.xiaoguan++;
            return;
        }
        self.eatOne(self.foods[self.isEatIndex],0,name);
    },
    eatAction2(name){
        let self = this;
        if(self.isEatIndex == self.foods1.length){
            return;
        }
        self.eatOne(self.foods1[self.isEatIndex],1,name);
    },
    eatAction3(name){
        let self = this;
        if(self.isEatIndex == self.foods2.length){
            return;
        }
        self.eatOne(self.foods2[self.isEatIndex],2,name);
    },

    eatOne(node,index,name){
        let self = this;
        var x;
        var y;
        var time;
        var time1;
        var time3;
        switch (self.isNow) {
            case 0:
                if(name == "left"){
                    x = 20;
                    y = 120;
                    time = 0.4;
                    time1 = 0.85;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    x = 0;
                    y = 70;
                    time = 1;
                    time1 = 1;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 1:
                if(name == "left"){
                    x = 15;
                    y = 135;
                    time = 0.4;
                    time1 = 0.85;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    x = 45;
                    y = 90;
                    time = 1;
                    time1 = 1;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 2:
                if(name == "left"){
                    x = 50;
                    y = 125;
                    time = 0.4;
                    time1 = 0.85;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    x = -20;
                    y = 85;
                    time = 1;
                    time1 = 1;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 3:
                if(name == "left"){
                    x = -95;
                    y = 155;
                    time = 0.4;
                    time1 = 0.85;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    x = 50;
                    y = 115;
                    time = 0.2;
                    time1 = 0.85;
                    time3 = 0.9;
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 4:
                if(name == "left"){
                    x = -80;
                    y = 155;
                    time = 0.4;
                    time1 = 0.85;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    x = 20;
                    y = 130;
                    time = 0.2;
                    time1 = 0.85;
                    time3 = 0.9;
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 5:
                if(name == "left"){
                    x = -75;
                    y = 155;
                    time = 0.4;
                    time1 = 0.85;
                    time3 = 0.9;
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    x = 45;
                    y = 130;
                    time = 0.2;
                    time1 = 0.85;
                    time3 = 0.9;
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 6:
                x = -200;
                y = 170;
                time = 0.9;
                time1 = 1;
                time3 = 1;
                var aindex = self.dongwulist[self.isNow][0];
                break;
            case 7:
                if(name == "left"){
                    x = 0;
                    y = 170;
                    time = 0.9;
                    time1 = 1;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    x = -440;
                    y = 150;
                    time = 0.8;
                    time1 = 1;
                    time3 = 1;
                    var aindex = self.dongwulist[self.isNow][0];
                }
                break;
        }
        // if(index == 1){
        //     x = -80;
        //     y = 150;
        //     time1 = 0.8;
        // }else if(index ==2){
        //     x = -470;
        //     y = 150;
        //     time = 0.55;
        //     time1 = 0.8;
        //     time3 = 0.7;
        // }
        actions.moveAndScale(node,time3,x,y,0.3,0.3,function () {
            actions.fadeOut(node,0.1);
            self.isEatIndex++;
            if(index == 0){
                self.eatAction(name);
            }else if(index == 1){
                self.eatAction2(name);
            }else {
                self.eatAction3(name);
            }

        });

        self.scheduleOnce(function(){
            dragons.playAnimation(self.dongwu[aindex],"eat",1,function () {
                dragons.playAnimation(self.dongwu[aindex],"happy",1,function () {
                    dragons.playAnimation(self.dongwu[aindex],"idle",0);
                });
            })
        },time);
        self.scheduleOnce(function(){
            soundMgr.playSoundEffectByName('chidongxi');
        },time1);


    },
    onDragSuccess(data){
        this.nowTime = -1;
        let self = this;
        var name = data.container.node.name;
        switch (self.isNow) {
            case 0:
                if(name == "left"){
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 1:
                if(name == "left"){
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 2:
                if(name == "left"){
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 3:
                if(name == "left"){
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 4:
                if(name == "left"){
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 5:
                if(name == "left"){
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    var aindex = self.dongwulist[self.isNow][1];
                }
                break;
            case 6:
                var aindex = self.dongwulist[self.isNow][0];
                break;
            case 7:
                if(name == "left"){
                    var aindex = self.dongwulist[self.isNow][0];
                }else {
                    var aindex = self.dongwulist[self.isNow][0];
                }
                break;
        }
        cc.audioEngine.pauseAll();
        nodeMgr.disableTouch();
        //成功的音效
        soundMgr.playSoundEffectByName('tongyong_right');
        //拖拽的节点
        //对的节点渐隐
        data.target.node.x = 0;
        data.target.node.y = -20;
        var name = data.target.node.name;
        self.isEatIndex = 0;
        // self.foods1 = data.target.node.children[0].children;
        // if(self.isNow == 5){
        //     name = data.container.node.name;
        // }
        // if(self.isNow > -1){

        if(self.isNow == 6){
            self.foods = data.target.node.children[0].children;
            dragons.playAnimation(self.dongwu[aindex],"happy",1,function () {
                dragons.playAnimation(self.dongwu[aindex],"idle",0);
            });
            for(var j = 0;j < self.yun[self.isNow].children[0].children.length;j++){
                dragons.playAnimation(self.yun[self.isNow].children[0].children[j],self.yun[self.isNow].children[0].children[j].name + "_shine",3);
            }
            soundMgr.playSoundEffectByName('cshuxue02_24',function () {
                self.yun[self.isNow].active = false;
                self.isEatIndex = 0;
                self.eatAction("left");
                soundMgr.playSoundEffectByName('5_left',function () {
                    self.nextGo();
                });
            });
            return;
        }
        if(data.container.node.name == "left"){
            self.foods1 = data.target.node.children[0].children;
            for(var j = 0;j < self.yun[self.isNow].children[0].children.length;j++){
                dragons.playAnimation(self.yun[self.isNow].children[0].children[j],self.yun[self.isNow].children[0].children[j].name + "_shine",3);
            }
            dragons.playAnimation(self.dongwu[aindex],"happy",1,function () {
                dragons.playAnimation(self.dongwu[aindex],"idle",0);
            });
        }else {
            self.foods2 = data.target.node.children[0].children;
            for(var j = 0;j < self.yun[self.isNow].children[1].children.length;j++){
                dragons.playAnimation(self.yun[self.isNow].children[1].children[j],self.yun[self.isNow].children[1].children[j].name + "_shine",3);
            }
            dragons.playAnimation(self.dongwu[aindex],"happy",1,function () {
                dragons.playAnimation(self.dongwu[aindex],"idle",0);
            });
        }
        if(self.level9Index == 0){
            self.level9Index++;
            nodeMgr.enableTouch();
            self.nowTime = (new Date()).getTime();
            return;
        }else {
            // self.foods2 = data.target.node.children[0].children;
            soundMgr.playSoundEffectByName('cshuxue02_24',function () {
                self.yun[self.isNow].active = false;
                self.isEatIndex = 0;
                self.eatAction2("left");
                soundMgr.playSoundEffectByName(self.isNow + '_left',function () {
                    self.isEatIndex = 0;
                    self.eatAction3("right");
                    soundMgr.playSoundEffectByName(self.isNow + '_right',function () {
                        self.nextGo();
                    })
                })
            });
            return;
        }
    },

    nextGo(){
        let self = this;
        if(self.isNow == 7){
            soundMgr.playSoundEffectByName('tongyong_bigright',function () {
                self._gameOver();
            });
            return;
        }
        self.isNow++;
        self.isEatIndex = 0;
        self.level9Index = 0;
        self._nextQuestion() ;
    },
    onDragWrong(data) {
        let self = this;
        //停止所有声音
        cc.audioEngine.pauseAll();
        //播放错误音效
        soundMgr.playSoundEffectByName('tongyong_error');
        //错误的节点抖动
        soundMgr.playSoundEffectByName('cshuxue02_23');
        actions.shake(data.target.node.children[0],0.3);
        self.nowTime = (new Date()).getTime();
        // if(self.isNow == 0){
        //     self.dongwu[self.isNow].active = false;
        //     self.ya2.opacity = 255;
        //     dragons.playAnimation(self.ya2,"sad",1,function () {
        //         self.dongwu[self.isNow].active = true;
        //         self.ya2.opacity = 0;
        //         dragons.playAnimation(self.dongwu[self.isNow],"Idle",0);
        //     });
        // }else {
        var dongwu_index;
        switch (self.isNow) {
            case 0:
                if(data.container.node.name == "left"){
                    dongwu_index = 0;
                }else {
                    dongwu_index = 1;
                }
                break;
            case 1:
                if(data.container.node.name == "left"){
                    dongwu_index = 0;
                }else {
                    dongwu_index = 1;
                }
                break;
            case 2:
                if(data.container.node.name == "left"){
                    dongwu_index = 0;
                }else {
                    dongwu_index = 1;
                }
                break;
            case 3:
                if(data.container.node.name == "left"){
                    dongwu_index = 2;
                }else {
                    dongwu_index = 3;
                }
                break;
            case 4:
                if(data.container.node.name == "left"){
                    dongwu_index = 2;
                }else {
                    dongwu_index = 3;
                }
                break;
            case 5:
                if(data.container.node.name == "left"){
                    dongwu_index = 2;
                }else {
                    dongwu_index = 3;
                }
                break;
            case 6:
                if(data.container.node.name == "left"){
                    dongwu_index = 4;
                }else {
                    dongwu_index = 4;
                }
                break;
            case 7:
                if(data.container.node.name == "left"){
                    dongwu_index = 4;
                }else {
                    dongwu_index = 4;
                }
                break;
        }
        if(self.isNow < 3 && data.container.node.name == "right"){
                self.dongwu[dongwu_index].active = false;
                self.ya2.opacity = 255;
                dragons.playAnimation(self.ya2,"sad",1,function () {
                    self.dongwu[dongwu_index].active = true;
                    self.ya2.opacity = 0;
                    dragons.playAnimation(self.dongwu[dongwu_index],"idle",0);
                });
        }else {
            dragons.playAnimation(self.dongwu[dongwu_index],"sad",1,function () {
                dragons.playAnimation(self.dongwu[dongwu_index],"idle",0);
            });
        }
    },

    update (dt) {
        let self = this;
        if(this.nowTime > 0){
            if(this.nowTime + 12000 < (new Date()).getTime()){
                if(!(nodeMgr.findNode("ui/backNode/gamePause").active)){
                    this.nowTime = (new Date()).getTime() + 12000;
                    for(var i = 0;i < self.yun[self.isNow].children.length;i++){
                        for(var j = 0;j < self.yun[self.isNow].children[i].children.length;j++){
                            dragons.playAnimation(self.yun[self.isNow].children[i].children[j],self.yun[self.isNow].children[i].children[j].name + "_shine",3);
                        }
                    }
                    soundMgr.playSoundEffectByName("cshuxue02_26");
                }
            }
        }
    },
});
